package com.it.you.controller.file;

import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * 上传进度监听器
 *
 * @author: YouHui
 * @Created: 2023/4/10 17:14
 */
@Component
public class FileUploadProgressListener implements ProgressListener {

    private HttpSession session;

    public void setHttpSession(HttpSession session) {
        this.session = session;
    }

    /**
     * @param bytesRead     到目前为止读取文件的比特数
     * @param contentLength 文件总大小
     * @param i             目前正在读取第几个文件
     */
    @Override
    public void update(long bytesRead, long contentLength, int i) {
        int percent = (int) (bytesRead * 100.0 / contentLength);
        session.setAttribute("percent", percent);
    }

}
